/**
 * Create by ray at 2015年4月30日 上午9:29:54.
 * Ray [email:rayinhangzhou@126.com]
 * GenerateBaseDaoMethod.java is for : 
 * TODO	
 *
 * Revision History
 *
 * Date@Programmer:	memo
 * ----------------------------------------------------------------
 * 2015年4月30日@Ray:	initial
 **/

package com.souche.employment.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class GenerateBaseDaoMethod {

    static String dbUrl = "jdbc:mysql://127.0.01:3306/employment?useUnicode=true&characterEncoding=utf8"
            + "&user=root&password=123123";
    static Connection connection = null;
    static Statement statement = null;
    static ResultSet resultSet = null;
    
    static String tableName = "user";
    static String domainName = "UserDO";
    static List<String> fieldlList = new ArrayList<>();
    static HashMap<String, String> mapFieldType = new HashMap<>();
    
    static void initConn() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(dbUrl);
        connection.setAutoCommit(true);
    }
    
    static String changeField(String field) {
        String domainField = "";
        String[] fields = field.split("_");
        if(fields.length==1) {
            return field;
        } else {
            domainField = fields[0];
            for (int i = 1; i < fields.length; i++) {
                domainField += fields[i].substring(0, 1).toUpperCase()+fields[i].substring(1);
            }
            return domainField;
        }
    }
    

    public static void main(String[] args) throws Exception {
        try {	
            initConn();
            statement = connection.createStatement();
            statement.setQueryTimeout(15);
            resultSet = statement.executeQuery("desc "+tableName);
            while (resultSet.next()) {
                String fieldName = resultSet.getString("Field");
                String fieldType = resultSet.getString("Type");
//                System.out.println(fieldName+"-->"+fieldType);
                fieldlList.add(fieldName.toLowerCase());
                mapFieldType.put(fieldName, fieldType.toLowerCase());
            }
            
//            System.out.println("private static final long	serialVersionUID	= 1L;");
            for (String field : fieldlList) {
                String domainField = changeField(field);
//                if(field.equals("id") ||field.equals("date_create") || field.equals("date_update") || field.equals("date_delete")) {
//                    continue;
//                }
                String fieldType = mapFieldType.get(field);
                if(fieldType.contains("char")) {
                    System.out.println("private String "+domainField+";");
                } else if (fieldType.contains("date") || fieldType.contains("time")) {
                	System.out.println("private Date "+domainField+";");
				} else if (fieldType.contains("bigint")) {
                	System.out.println("private Long "+domainField+";");
				} else if (fieldType.contains("int")) {
                	System.out.println("private Integer "+domainField+";");
				} else if (fieldType.contains("text")) {
                	System.out.println("private String "+domainField+";");
				} else {
                	System.out.println("private Integer "+domainField+";");
				}
            }
//        	System.out.println("public "+domainName+"() {}");
//          System.out.println();
            
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            connection.close();
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}
